package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;

/**
 * 处理相册相关请求的控制器
 *
 * @author 何惠民
 * @version 0.0.1
 */
@Validated
@Api(tags = "01相册管理模块")
@Slf4j
@RestController
@RequestMapping( "/albums")
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    public AlbumController() {
        log.debug("创建控制器对象:AlbumController");
    }
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    //                            ↓↓↓↓↓↓以下是检查参数的注解
    public JsonResult<Void> addNewAlbum(@Valid AlbumAddNewDTO albumAddNewDTO){
        log.debug("开始处理[添加相册]的请求,参数{}",albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        log.debug("添加相册数据成功");
        return JsonResult.ok();
    }

//    @Pattern(regexp = "")
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "相册id", required = true ,dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@Range(min = 1, message = "删除相册失败,删除的相册ID无效")
                             @PathVariable Long id){
        log.debug("开始处理[删除相册]的请求,参数:{}",id);
        albumService.deleteById(id);
        log.debug("删除Id为{}的相册成功",id);
        return JsonResult.ok();
    }

    @ApiOperation("修改相册")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void>  update(AlbumUpdateDTO albumUpdateDTO){
        log.debug("开始处理[修改相册]的请求,参数{}",albumUpdateDTO);
        albumService.update(albumUpdateDTO);
        log.debug("修改测试数据成功");
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询相册详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<AlbumStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理[根据id查询相册]的请求,参数{}",id);
        AlbumStandardVO albumStandardVO = albumService.getStandardById(id);
        log.debug("查询相册成功查询到{}",albumStandardVO);
        return JsonResult.ok(albumStandardVO);
    }


    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AlbumListItemVO>> list(){
        log.debug("开始处理[查询相册列表]的请求,参数无");
        List<AlbumListItemVO> list = albumService.list();
        log.debug("查询相册列表成功");
        return JsonResult.ok(list);
    }

    @ApiOperation("[已过期]根据名称删除相册")
    @ApiOperationSupport(order = 900)
    @PostMapping("/{name:[a-z]+}/delete")
    public String deleteByName(@PathVariable String name){
        String message = "接到了来自客户端尝试测试删除名称为["+name+"]相册的请求";
        log.debug(message);
        return message;
    }
    @ApiOperation("[已过期]删除相册测试")
    @ApiOperationSupport(order = 900)
    @PostMapping("/test/delete")
    public String delete(){
        String message = "接到了来自客户端尝试删除相册的请求";
        log.debug(message);
        return message;
    }
}
